<template>
  <div>
    <div>
      <el-row :gutter="20">
        <el-col :span="6">
          <div class="form-item">
            <span class="title">优惠券名称</span>
            <el-input placeholder="请输入名称" v-model="param.data.name"></el-input>
          </div>
        </el-col>
        <el-col :span="6">
          <div class="form-item">
            <span class="title">有效日期</span>
            <el-date-picker
              v-model="EffectivedateList"
              type="daterange"
              value-format="yyyy-MM-dd"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :clearable="false"
            ></el-date-picker>
          </div>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="6">
          <div class="form-item">
            <span class="title">创建日期</span>
            <el-date-picker
              v-model="createdateList"
              type="daterange"
              value-format="yyyy-MM-dd"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :clearable="false"
            ></el-date-picker>
          </div>
        </el-col>
        <el-col :span="6">
          <div class="form-item button-container">
            <el-button type="primary" @click="search" icon="el-icon-search">查询</el-button>
            <el-button type="primary" @click="resetSearchCotnet" icon="el-icon-refresh">重置</el-button>
            <el-button
              type="primary"
              @click="addCoupon"
              v-if="isPermissionsBtn('giftActivity_add')"
              icon="el-icon-circle-plus-outline"
            >新增</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <div class="table-body">
      <el-table :data="tableData" border v-loading="isLoading" style="width: 100%">
        <el-table-column prop="couponName" label="优惠卷名称"></el-table-column>
        <el-table-column prop="type" label="优惠券类型"></el-table-column>
        <el-table-column prop="createTime" width="180" label="创建时间"></el-table-column>
        <el-table-column prop="startTime" width="180" label="开始时间"></el-table-column>
        <el-table-column prop="endTime" width="180" label="结束时间"></el-table-column>
        <el-table-column prop="consumername" label="创建人"></el-table-column>
        <el-table-column prop="tock" label="数量"></el-table-column>
        <el-table-column fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-button
              @click="handleToDetial(scope.row)"

              v-if="isPermissionsBtn('giftActivity_detail')"
              size="small"
            >详情</el-button>
            <el-popconfirm title="确定删除此活动？" @onConfirm="handleClickDelete(scope.row)">
              <el-button
                style="margin-left:15px"
                size="small"
                type="danger" 
                v-if="isPermissionsBtn('giftActivity_del')"
                slot="reference"
              >删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          @current-change="currentPageChange"
          layout="prev, pager, next"
          :total="total"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import { mapGetters } from "vuex";
export default {
  name: "",
  data() {
    return {
      param: {
        data: {
          name: "", // 优惠券名称
          type: "4", //类型
          startTimes: "", //有效开始日期
          endTimes: "", //有效结束日期
          startCreateTime: "", // 创建开始日期
          endCreateTime: "" // 创建开始日期
        },
        queryRequest: {
          pageNum: 1,
          pageSize: 10
        }
      },
      EffectivedateList: [],
      createdateList: [],
      isLoading: true,
      tableData: [],
      total: 1
    };
  },
  components: {},
  computed: {
    ...mapGetters(["isPermissionsBtn"])
  },
  beforeMount() {},
  mounted() {
    this.initData();
  },
  methods: {
    initData() {
      this.isLoading = true;
      if (this.EffectivedateList.length > 0) {
        this.param.data.startTimes = this.EffectivedateList[0];
        this.param.data.endTimes = this.EffectivedateList[1];
      }
      if (this.createdateList.length > 0) {
        this.param.data.startCreateTime = this.createdateList[0];
        this.param.data.endCreateTime = this.createdateList[1];
      }
      this.$service.getcouponLists(this.param).then(res => {
        this.isLoading = false;
        if (res.code == "00000") {
          if (
            res.data.rows.length == 0 &&
            this.param.queryRequest.pageNum != 1
          ) {
            this.param.queryRequest.pageNum -= 1;
            this.initData();
          }
          this.tableData = (res.data && res.data.rows) || [];
          this.total = res.data.total || 0;
        }
      });
    },
    search() {
      this.param.queryRequest.pageNum = 1;
      this.initData();
    },
    resetSearchCotnet() {
      //重置搜索条件
      this.param.data.name = "";
      this.param.data.startTimes = "";
      this.param.data.endTimes = "";
      this.param.data.startCreateTime = "";
      this.param.data.endCreateTime = "";
      this.EffectivedateList = [];
      this.createdateList = [];
    },
    addCoupon() {
      this.$router.push({
        path: "/giftActivityAdd"
      });
    },
    currentPageChange(e) {
      this.param.queryRequest.pageNum = e;
      this.initData();
    },
    handleToDetial(scope) {
      this.$router.push({
        path: "/giftActivityDetail",
        query: { id: scope.couponId }
      });
    },
    handleClickDelete(scope) {
      this.$service.couponDelete(scope.couponId).then(res => {
        if (res.code == "00000") {
          this.$message({
            message: "删除成功",
            type: "success"
          });
          this.initData();
        } else {
          this.$message({
            message: res.message,
            type: "warning"
          });
        }
      });
    },
  },
  watch: {}
};
</script>
<style scoped>
</style>

